<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license        
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------
/**
 * Events
 *
 * @package         conlabz CMS
 * @subpackage      Controllers
 */
class Events extends User_Controller {
	function Events( ) {
		parent::User_Controller( );
		
		$this->load->helper( 'form' );

		// Check for access permission
		// needs to be defined as a resource over the admin area
		check( 'Events' );

		// Load the validation library
		$this->load->library( 'validation' );

		// Load models
		$this->load->model( 'events_model' );
		$this->load->model( 'categories_model' );
		$this->load->model( 'location_model' );
		$this->load->module_model( 'article', 'website_model' );
		
		// Load the events module
		$this->lang->module_load( 'events', 'events' );

		// Set breadcrumb
		$this->bep_site->set_crumb( $this->lang->line( 'events' ), 'events/user/events' );
				
		log_message( 'debug', 'conlabz CMS : Events class loaded' );
	}	
	
	/**
	 * View Events
	 *
	 * @access public
	 */
	function index( ) {
		// Get events
		$data['events'] = $this->events_model->getEvents( );
		
		// Display Page
		$data['header'] = $this->lang->line( 'events_overview' );
		$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/view_events";
		$data['menu'] = $this->website_model->getWebsiteUserMenu( );
		$data['module'] = 'events';
		
		$this->load->module_view( 'article', 'user/container', $data );
	}
		
	/**
	 * Get Event Details
	 *
	 * Load blog detail values from the submitted form
	 *
	 * @access private
	 * @return array
	 */
	function _get_event_details( ) {
		$data['id']                   = $this->input->post( 'id'                   );
		$data['type']       	      = $this->input->post( 'type'                 );
		$data['title']                = $this->input->post( 'title'                );
		$data['active']               = $this->input->post( 'active'               );
		$data['weekday']              = $this->input->post( 'weekday'              );
		$data['end_date']             = $this->input->post( 'end_date'             );
		$data['end_time']             = $this->input->post( 'end_time'             );
		$data['location']       	  = $this->input->post( 'location'             );
		$data['highlight']            = $this->input->post( 'highlight'            );
		$data['start_date']           = $this->input->post( 'start_date'           );
		$data['start_time']        	  = $this->input->post( 'start_time'           );
		$data['description']       	  = $this->input->post( 'description'          );
		$data['short_description']    = $this->input->post( 'short_description'    );
			
		return $data;
	}
	
	/**
	 * Display Event Form
	 *
	 * @access public
	 * @param integer $id Event ID
	 */
	function form( $id = NULL ) {
		$this->bep_assets->load_asset( 'jquery.datePicker' );
		$this->bep_site->set_js_block( '$(function(){ 
											$(\'.date-pick\').datePicker();	
										});' );
		
		// VALIDATION FIELDS
		$fields['id']                = "ID";
		$fields['type'] 	         = $this->lang->line( 'event_type'              );
		$fields['title']             = $this->lang->line( 'event_title'             );
		$fields['active']            = $this->lang->line( 'event_active'            );
		$fields['weekday']           = $this->lang->line( 'event_weekday'           );
		$fields['end_date']          = $this->lang->line( 'event_end_date'          );
		$fields['end_time']          = $this->lang->line( 'event_end_time'          );
		$fields['location']   		 = $this->lang->line( 'event_location'          );
		$fields['highlight']         = $this->lang->line( 'event_highlight'         );
		$fields['start_date']        = $this->lang->line( 'event_start_date'        );
		$fields['start_time']        = $this->lang->line( 'event_start_time'        );
		$fields['description']       = $this->lang->line( 'event_description'       );
		$fields['short_description'] = $this->lang->line( 'event_short_description' );
			
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $id ) ) {
			// Use create website rules (make sure no-one has the same short name)
			$rules['type']		        = "trim|required";
			$rules['title']             = "trim|required";
			$rules['weekday']           = "trim|required";
			$rules['end_date']          = "trim|required";
			$rules['end_time']          = "trim|required";
			$rules['location']		    = "trim|required";
			$rules['start_date']        = "trim|required";
			$rules['start_time']        = "trim|required";
			$rules['description']       = "trim|required";
			$rules['short_description'] = "trim|required";
		} else {                                  
			// Use edit website rules (make sure no-one other than the current user has the same short name)
			$rules['type']		        = "trim|required";
			$rules['title']             = "trim|required";
			$rules['weekday']           = "trim|required";
			$rules['end_date']          = "trim|required";
			$rules['end_time']          = "trim|required";
			$rules['location']		    = "trim|required";
			$rules['start_date']        = "trim|required";
			$rules['start_time']        = "trim|required";
			$rules['description']       = "trim|required";
			$rules['short_description'] = "trim|required";
		}
		
		// Setup form default values
		if( !is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			$events = $this->events_model->getEvents( array( 'event.id' => $id ) );
			$events = $events->row_array( );
			$this->validation->set_default_value( 'events', $events['id'] );
			$this->validation->set_default_value( $events );
		} elseif( is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Create form, first load
			$this->validation->set_default_value( 'category', $this->preference->item( 'default_events_category' ) );
			$this->validation->set_default_value( 'active', '1' );
			$this->validation->set_default_value( 'highlight', '1' ); 
		} elseif( $this->input->post( 'submit' ) ) {
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// RUN
		if( $this->validation->run( ) === FALSE) {
			// Construct  dropdowns
			$data['type']     = $this->categories_model->buildCategoriesDropdown( );
			$data['location'] = $this->location_model->buildLocationsDropdown( );
			
			$this->validation->output_errors( );
			$data['fckeditor'] = array(
              	'name'        => 'description',
              	'id'          => 'description',
              	'toolbarset'  => 'Default',
              	'basepath'    => DEVPATH.'assets/plugins/fckeditor/',
              	'width'       => '60%',
              	'height'      => '300',
				'config'	  => '/template/'.$this->session->userdata( 'template' ).'/js/fckeditor.js'
    		);
			
			$this->validation->output_errors( );
			$data['header'] = (is_null( $id ) ? $this->lang->line( 'create_event' ) : $this->lang->line( 'edit_event' ) );
			$this->bep_site->set_crumb( $data['header'], 'events/user/events/form/'.$id );
			$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/form_events";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'events';
			$this->load->module_view( 'article', 'user/container', $data );
		} else {
			// Save form
			if( is_null( $id ) ) {
				// CREATE
				// Fetch form values
				$events = $this->_get_event_details( );
			
				$this->db->trans_start( );
				$events['event_id'] = $this->events_model->insert( $events );
				
				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $events['title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_create_user' ) ) );
				}
				redirect( 'events/user/events' );
			} else {
				// SAVE
				$events = $this->_get_event_details( );
				$this->db->trans_start( );
				$this->events_model->update( $events, $events['id'], 'events' );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $events['title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_user' ) ) );
				}
				redirect( 'events/user/events' );
			}
		}
	} // form( );
	
	/**
	 * Delete
	 *
	 * Delete the selected events from the system
	 *
	 * @access public
	 */
	function delete( ) {
		if( FALSE === ( $selected = $this->input->post( 'select' ) ) ) {
			redirect( 'events/user/events', 'events' );
		}

		foreach( $selected as $events ) {
			$this->db->delete( 'event', array( 'id' => $events ) );
		}

		flashMsg( 'success', $this->lang->line( 'event_delete_confirm' ) );
		redirect( 'events/user/events', 'events' );
	}
}
/* End of file events.php */
/* Location: ./modules/events/controllers/user/events.php */